package com.markus.code.剑指offer;

import java.util.HashSet;
import java.util.Set;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/31 13:33
 * todo: 最长不含重复字符串的子串
 */
public class LengthOfLongestSubstring {
    /**
     * 利用滑动窗口来做
     */
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0){
            return 0;
        }
        //利用Set来存储窗口里的字符
        Set<Character> set = new HashSet<>();
        int r = -1;
        int max = 0;
        char[] strs = s.toCharArray();
        for (int i=0;i<strs.length;i++){
            if (i != 0){
                set.remove(strs[i-1]);
            }
            while(r+1 < strs.length && !set.contains(strs[r+1])){
                set.add(strs[r+1]);
                r++;
            }
            max = Math.max(max,set.size());
        }
        return max;
    }

    public static void main(String[] args) {
        String s = "abcabcbb";
        LengthOfLongestSubstring demo = new LengthOfLongestSubstring();
        System.out.println(demo.lengthOfLongestSubstring(s));
    }
}
